const sequelize = require('../db/dbconfig')
const Sequelize = require('sequelize')
const Op = Sequelize.Op
const PostLikeModel = require('../db/models/post_likes')
const PostinfoModel = require('../db/models/posts')
const postLikeModel = PostLikeModel(sequelize)
const postinfoModel = PostinfoModel(sequelize)

// 增
exports.addPostLike = async (req ,res) => {
  try{
    const post = req.params
    const user = req.auth
    let is_exist = await postinfoModel.findOne({
      attributes: ['like_num'],
      where: {
        is_delete: 0,
        post_id: post.id
      },
      raw: true
    })
    if(is_exist === null) return res.myMsg('点赞失败，文章不存在')
    let addLike = await postinfoModel.update({
      like_num: is_exist.like_num+1
    },{
      where: {
        is_delete: 0,
        post_id: post.id
      }
    })
    let results = await postLikeModel.create({
      user_id: user.user_id,
      post_id: post.id,
      raw:true
    })
    res.myMsg(results,0)
  }catch (e) {
    res.myMsg(e)
  }
}